import { selectRecord, updateRecordState } from "../../../../../service/api/api-assess-record";
import Message from 'tdesign-miniprogram/message/index';
// pages/assess/render/record/DetailsRecord/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //用户信息
    currentUser: {},//当前用户
    access: {} as any,// 当前用户权限
    //考核详情数据
    recordId: 0,//考核列表id
    recordTitle: '',//考核列表title
    recordDetail: {},//考核详情
    previewDataUrlType: 'analysis',//跳转原文的时候的type
    recordState: 0,//考核状态
    viewshowConfirm: false,//通过审核的确认框是否打开
    recordScore: 0,//考核分数
    //预览附件
    previewData: {},//预览图片信息
    imageList: [],//现有的图片列表
    viewPreviewImageVisible: false,//是否预览图片
  },
  //预览图片
  async onClickPreviewImage(e) {
    const file = e.currentTarget.dataset.file
    const type = e.currentTarget.dataset.type
    const index1 = e.currentTarget.dataset.index1
    const index2 = e.currentTarget.dataset.index2
    if (!type.includes('image')) {
      // const response = await attachmentPreview({
      //   id: file.id,
      // });
      // if (response.code === 0) {
      //   this.setData({
      //     previewData: {
      //       title: file.name,
      //       onlinePreview: true,
      //       url: file.content,
      //       content: response.data,
      //     }
      //   })
      //   wx.navigateTo({
      //     url: '/pages/web/index?url=' + encodeURIComponent(this.data.previewData.content) + '&title=' + this.data.previewData.title + '&type=' + this.data.previewDataUrlType
      //   })
      // } else {
      //   wx.showToast({
      //     title: '暂时无法预览',
      //     icon: 'error'
      //   })
      // }
    } else {
      const List = this.data.recordDetail.recordAttachments[index1].attachments.filter((item: any) => {
        if(item.mimeType!==null){
          return item.mimeType.includes("image")
        }else{
          return
        }
      });
      const imageList = List.map((item) => item.content)
      this.setData({
        imageList,
        viewPreviewImageVisible: true
      })
    }
  },
  //关闭图片预览
  onClosePreviewImage(e) {
    this.setData({
      viewPreviewImageVisible: false,
    });
  },
  //加载详情
  async onLoadDetail(id: number) {
    try {
      const response = await selectRecord({ id });
      if (response.code === 0) {
        this.setData({
          recordDetail: response.data,
          recordState: response.data.state,
          recordScore: response.data.isApplyScore === 0 ? response.data.sourceScore : response.data.applyScore,
        })
      }
    } catch (error) { }
  },
  //跳转原文
  navigateToArticle(e) {
    const url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: '/pages/web/index?url=' + encodeURIComponent(url) + '&type=' + this.data.previewDataUrlType
    })
  },
  //打开是否通过的确认框
  openConfirm() {
    this.setData({
      viewshowConfirm: true
    })
  },
  //点击确认后的弹窗关闭
  closeConfirmDialog() {
    this.setData({
      viewshowConfirm: false
    })
  },
  //输入最终分数
  onFinalScoreInput(e) {
    this.setData({
      recordScore: e.detail.value
    })
  },
  //通过此考核
  async onSubmitPass() {
    getApp().preventActive(async () => {
      const response = await updateRecordState({ id: this.data.recordId, finalScore: this.data.recordScore, state: 3 });
      if (response.code === 0) {
        Message.success({
          context: this,
          offset: [20, 32],
          duration: 5000,
          content: '提交成功',
        });
      }
      this.closeConfirmDialog()
      this.onLoadDetail(this.data.recordId)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: { id: any; title: any; }) {
    const { globalData: { access, currentUser } } = getApp();
    this.setData({
      access,
      currentUser
    })
    this.setData({
      recordTitle: options.title,
      recordId: options.id
    })
    this.onLoadDetail(options.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})